package com.tongji.seller.view.renderer
{
	import alternativa.engine3d.loaders.collada.collada;
	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.tongji.seller.events.utilsEvent.ListItemMouseEvent;
	import com.tongji.seller.model.StoreProductModel;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author chenzhongyuan
	 * 2011-12-10
	 */
	
	public class EditProductItemRendererMediator extends Mediator
	{
		[Inject]
		public var view:EditProductItemRenderer;
		
		[Inject]
		public var model:StoreProductModel;
		
		[Embed(source="assets/images/loading.swf")]
		private var EmbedLoading:Class;
		
		private var loadingMC:MovieClip;
		private var loader:Loader;
		private var bulkLoader:BulkLoader;
		private var object:Object;
		private var array:ArrayCollection = new ArrayCollection();

		override public function onRegister():void
		{
			//view.enabled = false;
			
			loadingMC = new EmbedLoading();
			loadingMC.x = 15;
			loadingMC.y = 5;
			if ( view.image.content == null)	
			{
				view.image.addChild(loadingMC);
				view.image.addEventListener(Event.COMPLETE, loadingCompleteHandler);
			}
			else
			{
				//view.image.removeChild(loadingMC);
				view.enabled = true;
			}
			
		}
		
		private function loadingCompleteHandler(event:Event):void
		{
			hideLoading();
			
			bulkLoader = new BulkLoader();
			bulkLoader.logLevel = BulkLoader.LOG_INFO;
			var length:int = model.storeProductArray.length;
			/*for (var i:int = 0; i < length; i ++)
			{*/
				if (view.data.ItemImgList != null)
				{
					if (view.data.ItemImgList.imgOne != null)
					{
						bulkLoader.add(view.data.ItemImgList.imgOne);
					}
					
					if (view.data.ItemImgList.imgTwo != null)
					{
						bulkLoader.add(view.data.ItemImgList.imgTwo);
					}
					
					if (view.data.ItemImgList.imgThree != null)
					{
						bulkLoader.add(view.data.ItemImgList.imgThree);
					}
				}		
			/*}*/
			bulkLoader.addEventListener(BulkLoader.COMPLETE, onAllItemsLoaded);
			bulkLoader.start();
			
		}
		
		public function onAllItemsLoaded(event:BulkProgressEvent):void
		{			
			view.enabled = true;
			event;
			bulkLoader.items[0].content;
			view.addEventListener(MouseEvent.MOUSE_DOWN, itemMouseDownHandler);	
			/*for ( var i:int = 0; i < event.target.items.length; i ++)
			{
				var bitmap:Bitmap = event.target.items[i].loader.content;
				model.uploadedProductArray.addItem(bitmap.bitmapData);
			}*/	
		}
		
		private function hideLoading():void
		{
			if (view.image.contains(loadingMC))
			{
				view.image.removeChild(loadingMC);
				/*view.enabled = true;*/
			}
		}
		
		private function itemMouseDownHandler(event:MouseEvent):void
		{
			view.addEventListener(MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);	
		}
		
		private function itemMouseMoveHandler(event:MouseEvent):void
		{
			view.removeEventListener(MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);
			var locate:int = view.parent.getChildIndex(view);
			view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, event, bulkLoader.items[0].content.bitmapData, locate));	
		}
	}
}